﻿using Cheems.HMI.Models;
using Cheems.HMI.ViewModels.Interface;
using Cheems.HMI.Views;
using Cheems.HMI.Views.UserManagement;
using Cheems.Interface.Configuration;
using Prism.Commands;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Cheems.HMI.ViewModels.Base
{
    public class UIEventVM : BindableBase, IUIEventVM
    {
        internal IRegionManager _regionManager;
        internal IContainerProvider _containerProvider;

        public UIEventVM(IRegionManager regionManager, IContainerProvider containerProvider)
        {
            this._regionManager = regionManager;
            this._containerProvider = containerProvider;

        }


        private DelegateCommand<string> _openWindow;
        public DelegateCommand<string> OpenWindowCommand =>
            _openWindow ?? (_openWindow = new DelegateCommand<string>(ExecuteOpenWindowCommand));

        void ExecuteOpenWindowCommand(string parameter)
        {
             OpenWindow(parameter);
        }

        LoginWindow loginWindow { get; set; }
        UserManagementWindow userManagementWindow { get; set; }

        public void OpenWindow(string parameter)
        {
            switch (parameter)
            {
                case "LoginWindow":
                    // 每次创建新实例
                    if (loginWindow == null || !loginWindow.IsVisible)
                    {
                        loginWindow = this._containerProvider.Resolve<LoginWindow>();
                    }
                    loginWindow.Owner = userManagementWindow;
                    loginWindow.Closed += (sender, e) =>
                    {
                        userManagementWindow.Activate();
                    };
                    loginWindow.Show();
                    break;

                case "UserManagementWindow":
                    if (userManagementWindow == null || !userManagementWindow.IsVisible)
                    {
                        userManagementWindow = this._containerProvider.Resolve<UserManagementWindow>();
                    }
                    userManagementWindow.Owner = this._containerProvider.Resolve<MainWindow>();
                    userManagementWindow.Closed += (sender, e) =>
                    {
                        this._containerProvider.Resolve<MainWindow>().Activate();
                    };
                    userManagementWindow.Show();
                    break;
            }


        }
    }
}
